﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json.Linq;

namespace DataIssue.Service
{
    /// <summary>
    /// 指数工具类(这个不能通过SG接口获取)
    /// </summary>
    public class IndexDataService
    {
        /// <summary>
        /// 获取所有需要查询的数据
        /// </summary>
        /// <returns></returns>
        public List<Entity.IndexData> GetIndexDatas()
        {
            List<Entity.IndexData> datas = GetDomesticDatas();
            var query = GetAbroadDatas();
            foreach (Entity.IndexData a in query)
            {
                datas.Add(a);
            }
            datas = datas.OrderBy(a => a.Sort).ToList();
            return datas;

        }

        public List<Entity.IndexData> GetPicIndexDatas()
        {
            List<Entity.IndexData> datas = new List<Entity.IndexData>();
            F10ParamEntity param = new F10ParamEntity("Quote", "FEED_HS_QUOTESNAP", 2);
            param.Filters = new List<string>();
            param.Filters.Add("STOCKCODE-in-str=000001,399001,000300");
            param.Filters.Add("SECURITYTYPE-int=4096");
            param.OrderBy = "TRADEDATE DESC,STOCKCODE ASC";

            JToken clientData;
            int totalCount = 0;
            F10Query.TryParse(param, false, out clientData, out totalCount);
            if (clientData != null && totalCount > 0)
            {
                for (int i = 0; i < clientData.Count(); i++)
                {
                    double temp = 0f;
                    Entity.IndexData data = new Entity.IndexData();
                    data.Code = clientData[i].J_String("STOCKCODE");
                    data.Name = GetQueryCodeName(data.Code);
                    data.Range = clientData[i]["CALC"].J_Double("PXCHGRATIO", temp) / 100;
                    data.Value = Math.Round(clientData[i].J_Double("LASTPX", temp) / 10000, 2);
                    string str = Convert.ToString(clientData[i]["VALUE"]);
                    string value = str.Length > 4 ? str.Substring(0, str.Length - 4) : str;
                    data.AllAmount = value;

                    data.Sort = GetQueryCodeSort(data.Code);

                    var query = datas.Where(a => a.Code.Equals(data.Code));
                    if (query.Count() == 0)
                    {
                        datas.Add(data);
                    }
                }
            }
            var tempString = datas[1];
            datas[1] = datas[2];
            datas[2] = tempString;
            return datas;
        }

        private List<Entity.IndexData> GetDomesticDatas()
        {
            List<Entity.IndexData> datas = new List<Entity.IndexData>();
            F10ParamEntity param = new F10ParamEntity("Quote", "FEED_HS_QUOTESNAP", 2);
            param.Filters = new List<string>();
            param.Filters.Add("STOCKCODE-in-str=000001,399001,000300,399006");
            param.Filters.Add("SECURITYTYPE-int=4096");
            param.OrderBy = "TRADEDATE DESC";

            JToken clientData;
            int totalCount = 0;
            F10Query.TryParse(param, false, out clientData, out totalCount);
            if (clientData != null && totalCount > 0)
            {
                for (int i = 0; i < clientData.Count(); i++)
                {
                    double temp = 0f;
                    Entity.IndexData data = new Entity.IndexData();
                    data.Code = clientData[i].J_String("STOCKCODE");
                    data.Name = GetQueryCodeName(data.Code);
                    data.Range = clientData[i]["CALC"].J_Double("PXCHGRATIO", temp) / 100;
                    data.Value = Math.Round(clientData[i].J_Double("LASTPX", temp) / 10000, 2);
                    data.Sort = GetQueryCodeSort(data.Code);

                    var query = datas.Where(a => a.Code.Equals(data.Code));
                    if (query.Count() == 0)
                    {
                        datas.Add(data);
                    }
                }
            }
            return datas;
        }

        private List<Entity.IndexData> GetAbroadDatas()
        {
            List<Entity.IndexData> datas = new List<Entity.IndexData>();
            F10ParamEntity param = new F10ParamEntity("Quote", "FEED_FEX_QUOTESNAP", 2);
            param.Filters = new List<string>();
            param.Filters.Add("CODE-in-str=FTLI,DQII,NSDI,JPII");

            JToken clientData;
            int totalCount = 0;
            F10Query.TryParse(param, false, out clientData, out totalCount);
            if (clientData != null && totalCount > 0)
            {
                for (int i = 0; i < clientData.Count(); i++)
                {
                    double temp = 0f;
                    Entity.IndexData data = new Entity.IndexData();
                    data.Code = clientData[i].J_String("CODE");
                    data.Name = GetQueryCodeName(data.Code);
                    data.Range = Math.Round(clientData[i].J_Double("PXCHG", temp) * 100 / clientData[i].J_Double("CLOSE", temp), 2);
                    data.Value = clientData[i].J_Double("NEW", temp);
                    data.Sort = GetQueryCodeSort(data.Code);
                    datas.Add(data);
                }
            }
            return datas;
        }

        private int GetQueryCodeSort(string code)
        {
            int sort = 0;
            switch (code)
            {
                case "000001":
                    sort = 0;
                    break;
                case "399001":
                    sort = 1;
                    break;
                case "000300":
                    sort = 2;
                    break;
                case "399006":
                    sort = 3;
                    break;
                case "FTLI":
                    sort = 4;
                    break;
                case "DQII":
                    sort = 5;
                    break;
                case "NSDI":
                    sort = 6;
                    break;
                case "JPII":
                    sort = 7;
                    break;
                default:
                    break;
            }
            return sort;
        }


        private string GetQueryCodeName(string code)
        {
            string name = "";

            switch (code)
            {
                case "000001":
                    name = "上证指数";
                    break;
                case "399001":
                    name = "深圳成指";
                    break;
                case "000300":
                    name = "沪深300";
                    break;
                case "399006":
                    name = "创业板";
                    break;
                case "FTLI":
                    name = "伦敦时报";
                    break;
                case "DQII":
                    name = "道琼斯";
                    break;
                case "NSDI":
                    name = "纳斯达克";
                    break;
                case "JPII":
                    name = "日经指数";
                    break;
                default:
                    break;
            }

            return name;
        }
    }
}